﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using uTrade.Data;

namespace uTrade.ViewModels
{
    public class FundDetailViewModel : INotifyPropertyChanged
    {
        public ICommand SyncFundDataCommand { get; set; }
        public ObservableCollection<Rank> Ranks { get; set; }

        public FundDetailViewModel()
        {
            SyncFundDataCommand = new DelegateCommand(SyncFundData, CansyncFundDataCommand);
            Ranks = new ObservableCollection<Rank>(GetFundEquityInfo.GetAllRankList());
        }

        private int _downloadProgress;

        public int DownloadProgress
        {
            get => _downloadProgress;
            private set
            {
                _downloadProgress = value;
                RaisePropertyChanged("DownloadProgress");
            }
        }

        public async void SyncFundData(object param)
        {
            GetFundRankInfo getMyRank = new GetFundRankInfo();
            Progress<double> progress = new Progress<double>();
            progress.ProgressChanged += (psender, args) =>
            {
                DownloadProgress = (int)args;
            };
            RankDAL.Clear();
            await getMyRank.SyncRankInfoAsync(progress);
            Ranks = new ObservableCollection<Rank>(GetFundEquityInfo.GetAllRankList());
        }

        public bool CansyncFundDataCommand(object param)
        {
            return true;
        }

        #region INotifyPropertyChanged属性

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion INotifyPropertyChanged属性

        #region 属性更改方法

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion 属性更改方法
    }
}